/*
 * Copyright (c) 2017. jiangyc <710183940@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.jiangyc.bingpictures.spi.impl;

import com.jiangyc.bingpictures.bean.Picture;
import com.jiangyc.bingpictures.spi.PictureLoader;
import com.jiangyc.bingpictures.util.JsoupUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

public class PlmeiziPictureLoader implements PictureLoader {
    /**
     * 图片数量
     */
    private int count = -1;
    // 图片集合，以图片的下标[0~count)作为键
    private Map<Integer, Picture> pictureMap = new HashMap<>();
    // Jsoup解析器
    private JsoupResover jsoupResover = new JsoupResover();

    /**
     * 图片数量
     *
     * @return
     */
    @Override
    public int count() {
        if (count == -1) {
            count = jsoupResover.count("http://bing.plmeizi.com/");
        }
        return count;
    }

    /**
     * 获取某一张图片，范围：[0, count)
     *
     * @param index
     * @return
     */
    @Override
    public Picture getPicture(int index) {
        Picture pic = pictureMap.get(index);

        if (pic == null) {
            String id = "" + (index + 1);
            pic = jsoupResover.getPicture(id);
            pictureMap.put(index, pic);
        }

        return pic;
    }

    /**
     * 获取某一张图片，范围：[0, count)
     *
     * @param index
     * @param orientation
     * @return
     */
    @Override
    public Picture getPicture(int index, String orientation) {
        Picture pic = getPicture(index);
        return setOrientation(pic, orientation);
    }

    /**
     * 是否是竖屏图片
     *
     * @param picture
     * @return
     */
    @Override
    public boolean isVertical(Picture picture) {
        return picture.getUrl().contains("1080x1920");
    }

    /**
     * 是否是横屏图片
     *
     * @param picture
     * @return
     */
    @Override
    public boolean isHorizontal(Picture picture) {
        return picture.getUrl().contains("1920x1080");
    }

    /**
     * 设置图片横屏竖屏模式
     *
     * @param picture
     * @param orientation
     * @return
     */
    @Override
    public Picture setOrientation(Picture picture, String orientation) {
        if (picture == null) {
            throw new NullPointerException("pic can not be null!");
        }
        String ori = (orientation == null) ? ORIENTATION_HORIZONTAL : orientation;

        if (ORIENTATION_HORIZONTAL.equals(ori) && isVertical(picture)) {
            picture.setUrl(picture.getUrl().replace("1080x1920", "1920x1080"));
        } else if (ORIENTATION_VERTICAL.equals(ori) && isHorizontal(picture)) {
            picture.setUrl(picture.getUrl().replace("1920x1080", "1080x1920"));
        }

        return picture;
    }

    static class JsoupResover {

        int count(String url) {
            String picUrl = JsoupUtils.elementAttr(url, "body > div.container > div.list > div > a:nth-child(1)", "href");

            String picId = picUrl.substring(picUrl.lastIndexOf("/") + 1);

            return Integer.parseInt(picId);
        }

        Picture getPicture(String id) {
            Picture pic = new Picture(id);
            String url = "http://bing.plmeizi.com/show/" + id;

            Document doc = JsoupUtils.connect(url);

            // date
            pic.setDate(doc.select("#date").text());
            // name
            String name = doc.title();
            name = name.substring(0, name.indexOf(" ("));
            pic.setName(name);
            // summary
            String summary = doc.select("#title").text();
            summary = summary.substring(summary.lastIndexOf(")") + 2);
            pic.setSummary(summary);
            // address
            pic.setUrl(JsoupUtils.elementAttr(doc, "#picurl", "href"));

            return pic;
        }
    }
}
